package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version
 * 1.0
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    AliyunSmsUtils aliyunSmsUtils;

    @Override
    public void sendShortMessage(String mobile, String type) {
        if(mobile == null || type == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //检验手机号是否可用
        if(!PhoneNumCheckUtils.isPhoneLegal(mobile)){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }

        //判断是否存在该手机号， 除了注册 都必须存在手机号
        if(type.equals(SMSConstant.SMS_TYPE_REGISTER) ){
            if(this.checkExists(mobile)){
                throw new NcException(ResponseEnum.USER_MOBILE_EMAIL_EXISTS);
            }
        } else{
            if(!this.checkExists(mobile)){
                throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
            }
        }


        //生成验证码
        String code = RandomUtil.randomNumbers(6);
        log.info("验证码为： " + code);
        //发送验证码
//        Boolean flag = aliyunSmsUtils.sendSMS(mobile, code);
//        if(!flag){
//            throw new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
//        }

        String key = getPrefix(type) + mobile;

        //存储到redis
        redisTemplate.boundValueOps(key).set(code, 5, TimeUnit.MINUTES);

    }

    @Override
    public Boolean verify(String mobile, String type, String checkcode) {
        if(mobile == null  || type == null || checkcode == null){
            throw new NcException(ResponseEnum.INVALID_VERIFY_CODE);
        }

        String key = getPrefix(type) + mobile;
        //获取真实的验证码
        Object realCode = redisTemplate.boundValueOps(key).get();
        //判断验证码是否一致
        if(realCode != null && realCode.equals(checkcode)){
            return true;
        }

        return false;
    }

    @Autowired
    SysUserMapper userMapper;

    public Boolean checkExists(String mobile){
        LambdaQueryWrapper<SysUser> userQw = new LambdaQueryWrapper<>();
        userQw.eq(SysUser::getMobile, mobile);

        Integer count = userMapper.selectCount(userQw);

        return count != 0;
    }


    public String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_TYPE_LOGIN;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_TYPE_REGISTER;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_TYPE_CHANGE_MANAGER;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_TYPE_RESET_PASSWORD;
            default:
                return null;
        }
    }
}
